package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pangda.common.domain.ResponseResult;
import com.pangda.pojo.Dept;
import com.pangda.pojo.dto.DeptDto;
import com.pangda.pojo.vo.NodeVo;
import com.pangda.pojo.vo.DeptVo;
import com.pangda.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/system/dept")
@Api(tags = "部门管理")
public class DeptController {


   @Autowired
   private IDeptService deptService;


   @GetMapping("/list")
   @ApiOperation("部门列表查询")
   public ResponseResult list(@RequestParam(value = "deptName",required = false) String deptName,
                    @RequestParam(value = "status",required = false) Integer status){
      LambdaQueryWrapper<Dept> wrapper =  new LambdaQueryWrapper<>();
      wrapper.eq(status != null,Dept::getStatus,status);
      wrapper.like(StringUtils.hasText(deptName),Dept::getDeptName,deptName);
      List<Dept> list = deptService.list(wrapper);
      list.stream().forEach(dept -> {
         list.stream().forEach(m->{
            if (dept.getParentId().equals(m.getDeptId())){
               dept.setParentName(m.getDeptName());
            }
         });
      });
      List<DeptVo> deptVos = BeanUtil.copyToList(list, DeptVo.class);
      return ResponseResult.success(deptVos);
   }

   @PostMapping
   @ApiOperation("新增部门")
   public ResponseResult add(@RequestBody DeptDto deptDto){
      Dept dept = BeanUtil.copyProperties(deptDto, Dept.class);
      Dept byId = deptService.getById(dept.getParentId());
      dept.setParentName(byId.getDeptName());
      dept.setCreateTime(LocalDateTime.now());
      return ResponseResult.success(deptService.save(dept));
   }

   @GetMapping("/{deptId}")
   @ApiOperation("根据id查询部门")
   public ResponseResult getById(@PathVariable("deptId") Integer deptId){
      Dept dept = deptService.getById(deptId);
      return ResponseResult.success(BeanUtil.copyProperties(dept, DeptVo.class));
   }

   @GetMapping("list/exclude/{deptId}")
   @ApiOperation("查询部门列表（排除本部门）")
   public ResponseResult get(@PathVariable Integer deptId){
      List<Dept> list = deptService.listExclude(deptId);
      return ResponseResult.success(list);
   }

   @PutMapping
   @ApiOperation("修改部门")
   public ResponseResult update(@RequestBody DeptDto deptDto){
      Dept dept = BeanUtil.copyProperties(deptDto, Dept.class);
      return ResponseResult.success(deptService.updateById(dept));
   }

   @DeleteMapping("/{deptId}")
   @ApiOperation("删除部门")
   public ResponseResult delete(@PathVariable("deptId") Integer deptId){
      return ResponseResult.success(deptService.removeById(deptId));
   }

   @GetMapping("/roleDeptTreeselect/{roleId}")
   @ApiOperation("根据角色id查询部门层级树")
   public ResponseResult getTreeById(@PathVariable Integer roleId){
      NodeVo node = deptService.getTreeById(roleId);
      return ResponseResult.success(node);
   }

   @GetMapping("/treeselect")
   @ApiOperation("查询部门层级树")
   public ResponseResult getTree() throws IOException {
      List<NodeVo> nodes = deptService.getTree();
      return ResponseResult.success(nodes);
   }

}
